#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 1e5+10;

int n,mod,a[N];
unordered_map<int,int> hstb;

int getphi(int x){
	if(hstb[x]) return hstb[x];
	int res = x;
	for(int i = 2;i * i <= x;i++){
		if(x % i == 0) res = res / i * (i-1);
		while(x % i == 0) x /= i;
	}if(x > 1) res = res / x * (x-1); return hstb[x] = res;
}

ll qp(ll x,ll t,int mod){ll res=1;for(;t;t>>=1,x=x*x) {if(x > mod) x = x % mod + mod;if(t&1)res=res*x;if(res > mod) res = res % mod + mod;}return res;}

ll work(int l,int r,int mod){
	if(l == r + 1|| mod == 1) return 1;
	return qp(a[l],work(l+1,r,getphi(mod)),mod);
}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in,mod = in;
	for(int i = 1;i <= n;i++) a[i] = in;
	int q = in;
	while(q--){
		int l = in,r = in;
		printf("%lld\n",work(l,r,mod)%mod);
	}
	return 0;
}
